package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/rotate-function/
 * @create 2025/10/20 11:49
 * @since JDK17
 */

public class Solution27 {
    public int maxRotateFunction(int[] nums) {
        int n = nums.length, ans, sum;
        int[] prefix = new int[n + 1], suffix = new int[n + 1];
        for (int i = 0; i < n; i++) {
            prefix[i + 1] = prefix[i] + nums[i];
        }
        sum = ans = prefix[n];
        for (int i= n - 1; i >= 0; i--) {
            suffix[i] = suffix[i + 1] + nums[i];
        }
        for (int i = 1; i < n; i++) {
            sum += prefix[n - i] - (n - i) * nums[n - i] + suffix[n - i + 1];
            System.out.println(sum);
            ans = Math.max(ans, sum);
        }

        return ans;
    }
}
